#include "stack.h"


stack::stack()
{

}

stack *stack::pushStack(stack *p, int elem)
{
    if(p == nullptr)
    {
        return nullptr;
    }
    else {
        Node * temp = new Node;
        temp->data = elem;
        temp->next = p->top;
        p->top = temp;
        p->count++;
        return p;
    }
}

stack *stack::popStack(stack *p)
{
    Node * temp = new Node;
    temp = p->top;
    if(p == nullptr)
    {
        cout << "The stack is null." << endl;
        return p;
    }
    else {
        p->top = p->top->next;
        delete temp;
        p->count--;
        return p;
    }
}

void stack::showStack(stack *s)
{
    Node * ptmp = new Node;
    ptmp = s->top;
    if(s->top == nullptr)
    {
        cout << "Error:this stack is null." << endl;
        return;
    }
    while( ptmp != nullptr)
    {
        cout << ptmp->data << endl;
        ptmp = ptmp->next;
    }

    return;
}

void testRunStack()
{
#ifdef StackLinkList
    stack * s = new stack;
    int input[5] = {11, 22, 33, 44, 55};
    for(int i = 0; i < 5; i++)
    {
        s->pushStack(s, input[i]);
    }
    s->showStack(s);
    s->popStack(s);
    s->showStack(s);
    delete s;
#else
    StackArray *s = new StackArray;
    int input[5] = {11, 22, 33, 44, 55};
    for(int i = 0; i < 5; i++)
    {
        s->push(s, input[i]);
    }
    s->printStack(s);
    s->pop(s);
    s->printStack(s);
    delete s;
#endif
}

StackArray::StackArray(){
    top = 0;
}

void StackArray::push(StackArray *s, int data)
{
    s->data[s->top] = data;
    s->top++;
}

void StackArray::pop(StackArray *s)
{
    if(s->top!=0)
    {
        s->data[s->top] = 0;
        s->top--;
    }
}

void StackArray::printStack(StackArray *s)
{
    for (int n = s->top-1; n>=0; n--) {
        cout << s->data[n] << endl;
    }
}
